/*
 * @lc app=leetcode.cn id=714 lang=cpp
 *
 * [714] 买卖股票的最佳时机含手续费
 */

// @lc code=start
class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int n = prices.size();
        vector<int>dp(2);
        dp[1] = -prices[0] - fee;
        for (int i = 1; i < n; ++i) {
            int tmp = dp[0];
            dp[0] = max(dp[0], dp[1] + prices[i]);
            dp[1] = max(dp[1], tmp - prices[i] - fee);
        }
        return dp[0];
    }
};
// @lc code=end

